﻿Imports Capa_de_Acceso_a_Datos
Public Class frmConsultarRecepciones
    Public farmaciaId As Integer
    Private comprobantesRecepcion As DataTable
    Private iniciando As Boolean = False

    Private Sub ConsultarRecepciones_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        iniciando = True
        dtpDesde.Value = New DateTime(Today.Year, 1, 1)
        dtpHasta.Value = Today
        cargarComboEmpleadosCafapro()
        actualizarComprobantesPresentacion()
        iniciando = False

    End Sub

    Private Sub cargarComboEmpleadosCafapro()
        cboEmpleadoCafapro.DataSource = EmpleadoDAO.comboCalendarioEmpleados
        cboEmpleadoCafapro.DisplayMember = "Descripcion"
        cboEmpleadoCafapro.ValueMember = "EmpleadoId"
        cboEmpleadoCafapro.SelectedValue = 0
    End Sub

    Private Sub cboEmpleadoCafapro_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEmpleadoCafapro.SelectedIndexChanged
        If Not iniciando Then
            actualizarComprobantesPresentacion()
        End If
    End Sub

    Private Sub actualizarComprobantesPresentacion()
        comprobantesRecepcion = ComprobantePresentacionDAO.filtroComprobantePresentaciones(farmaciaId, cboEmpleadoCafapro.SelectedValue, dtpDesde.Value, dtpHasta.Value)
        dgvComprobantesRecepcion.DataSource = comprobantesRecepcion
        dgvComprobantesRecepcion.Refresh()
        colorearGrilla()
    End Sub

    Private Sub colorearGrilla()
        With dgvComprobantesRecepcion
            ' alternar color de filas  
            .AlternatingRowsDefaultCellStyle.BackColor = Color.FloralWhite
            .DefaultCellStyle.BackColor = Color.Beige
        End With
    End Sub

    Private Sub dtpDesde_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpDesde.ValueChanged
        If Not iniciando Then
            actualizarComprobantesPresentacion()
        End If
    End Sub

    Private Sub dtpHasta_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpHasta.ValueChanged
        If Not iniciando Then
            actualizarComprobantesPresentacion()
        End If
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        frmSeleccionarFarmacia.frmLlamada = Me.Name
        frmSeleccionarFarmacia.ShowDialog()
        actualizarComprobantesPresentacion()
    End Sub

    Private Sub btnNueva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        frmRegistrarRecepcionPresentacion.ShowDialog()
        actualizarComprobantesPresentacion()
    End Sub

    Private Sub btnVisualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisualizar.Click

        If HayFilaSeleccionada() Then
            visualizarComprobante()
        Else
            MsgBox("Seleccione primero una fila", MsgBoxStyle.Exclamation)
        End If
    End Sub

    Private Function HayFilaSeleccionada() As Boolean
        Dim hay As Boolean = False

        If dgvComprobantesRecepcion.SelectedRows.Count > 0 Then
            hay = True
        End If

        Return hay
    End Function

    Private Sub dgvComprobantesRecepcion_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvComprobantesRecepcion.DoubleClick
        visualizarComprobante()
    End Sub

    Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
        Me.Dispose()
    End Sub

    Private Sub visualizarComprobante()
        frmComprobanteRecepcion.idComprobantePresentacion = CInt(dgvComprobantesRecepcion.SelectedRows(0).Cells(0).Value)
        frmComprobanteRecepcion.ShowDialog()
    End Sub

End Class